<?php 
Class articleController extends appController
{
	/**
	 * 显示文章列表
	 */
	public function index()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$sort['cat_id'] = yf_empty(intval($_GET['c']), "", true);
		$sort['keyword'] = yf_empty(trim($_GET['k']), '', true);
		
		$art_list = Model('article')->art_list($sort, $page);
		$cat_sort['type'] = array(1, 2, 3);
		$this->assign('cat_list', Model('menu')->get_cat_list($cat_sort));
		$this->assign('art_list', $art_list['data']);
		$this->assign('art_page', $art_list['pagelink']);
		$this->assign('cat_id', $sort['cat_id']);
		$this->display('index.html');
	}
	
	/**
	 * 文章排序
	 */
	public function ordering()
	{
		$this->admin_priv('order_news');
		$id = yf_empty(intval($_POST['id']), '', true);
		$val = yf_empty(intval($_POST['val']), '', true);
		$result = Model('article')->ordering($val, $id);
		$this->showMsg($result);
	}
	
	/**
	 * 删除文章及其图片
	 */
	public function del()
	{
		$this->admin_priv('del_news', 2);
		$id = yf_empty($_REQUEST['id'], '', true);
		$result = array('error' => 1, 'content' => '删除失败');
		$art = Model('article');
		$res = $art->del(array('table' => 'article', 'id' => $id));
		$res2 = $art->del(array('table' => 'album_photos', 'aid' => $id));
		if($res&&$res2)
		{
		 	$result['error']=0;
		 	$result['content']='删除成功';
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示文章编辑页
	 */
	public function edit_art()
	{
		$this->admin_priv('edit_news', 2);
		$this->assign('forms_list', Model('forms')->get_all_forms());
		$this->assign('parent_list', Model('menu')->get_cat_list());
		$id = yf_empty(intval($_GET['id']), '', true);
		$this->assign('id', $id);
		$art_info = Model('article')->get_art($id);
		$this->assign('art', $art_info);
		$this->assign('action', 'modified_art');
		$this->display('article.html');
	
	}
	/**
	 * 更新文章数据
	 */
	public function modified_art()
	{
		$this->admin_priv('edit_news');
		$data = array('error' => 1, 'content' => '');
		$id = yf_empty(intval($_POST['id']), "系统错误");
		$info['cat_id'] = yf_empty($_POST['cat_id'], '', true);
		
		if($_POST['summary'] === '' && $_POST['content'] != '')
		{
			$_POST['summary'] = hy_substr(str_replace(PHP_EOL, '', strip_tags($_POST['content'])), 200);
		}
		if($_POST['en_summary'] === '' && $_POST['en_content'] != '')
		{
			$_POST['en_summary'] = hy_substr(str_replace(PHP_EOL, '', strip_tags($_POST['en_content'])), 200);
		}
		$en_summary = yf_empty($_POST['en_summary'], '', true);
		$info['flags'] = is_array($_POST['flags']) ? implode(',', $_POST['flags']) : $_POST['flags'];
		$info['summary'] = isset($_POST['summary']) ? CH_SC.$_POST['summary'].DIG_EC.EN_SC.$en_summary.DIG_EC : '';
		$en_title = yf_empty($_POST['en_title'], '', true);
		$en_author = yf_empty($_POST['en_author'], '', true);
		$en_content = yf_empty($_POST['en_content'], '', true);
		$title = yf_empty($_POST['title'], Lang('标题不能为空'));
		$info['title'] = isset($_POST['title']) ? CH_SC.$title.DIG_EC.EN_SC.$en_title.DIG_EC : '';
		$info['abstract'] = yf_empty($_POST['abstract'], '', true);
		$info['keywords'] = yf_empty($_POST['keywords'], '', true);
		$info['author'] = isset($_POST['author']) ? CH_SC.$_POST['author'].DIG_EC.EN_SC.$en_author.DIG_EC : '';
		$info['source'] = yf_empty($_POST['source'], '', true);
		$info['content'] = isset($_POST['content']) ? CH_SC.$_POST['content'].DIG_EC.EN_SC.$en_content.DIG_EC : '';
		//$info['ordering'] = yf_empty($_POST['ordering'], 0, true);
		//$info['hits']=isset($_POST['hits'])?$_POST['hits']:'';
		$info['is_show'] = yf_empty($_POST['is_show'], 1, true);
		$info['source_url'] = yf_empty($_POST['source_url'], '', true);
		$info['page_title'] = yf_empty($_POST['page_title'], '', true);
		$info['created']= empty($_POST['created']) ? yf_time() : strtotime($_POST['created']);
		$info['thumb'] = yf_empty($_POST['thumb'], '', true);
		$info['form_id'] = yf_empty($_POST['form_id'], 0, true);
		
		if(!empty($_POST['photo']))
		{
			$photo = is_array($_POST['photo']) ? $_POST['photo'] : array($_POST['photo']);
		}
		else 
		{
			$photo = array();
		}
		$info['modified'] = yf_time();
		$result=Model('article')->update_art($info, $photo, $id);
		
		if($result)
		{
			$data['content']=Lang('SUCCESS_EDIT');
			$data['error']=0;
			
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
	/**
	 * 发布文章
	 */
	public function add_art()
	{
		$this->admin_priv('add_news', 2);
		$this->assign('forms_list', Model('forms')->get_all_forms());
		$this->assign('parent_list', Model('menu')->get_cat_list());
		$cat_id = yf_empty(intval($_GET['cat_id']), '', true);
		$this->assign('cat_id', $cat_id);
		$this->assign('action', 'new_art');
		$this->display('article.html');
	}
	
	/**
	 * 添加文章数据
	 */
	public function new_art()
	{
		$this->admin_priv('add_news');
		$data = array('error' => 1, 'content' => '');
		
		$info['cat_id'] = yf_empty(intval($_POST['cat_id']), '', true);
		//自动截取summary
		if($_POST['summary'] === '' && $_POST['content'] != '')
		{
			$_POST['summary'] = hy_substr(str_replace(PHP_EOL, '', strip_tags($_POST['content'])), 200);
		}
		if($_POST['en_summary'] === '' && $_POST['en_content'] != '')
		{
			$_POST['en_summary'] = hy_substr(str_replace(PHP_EOL, '', strip_tags($_POST['en_content'])), 200);
		}
		$en_summary = yf_empty($_POST['en_summary'], "", true);
		$info['summary'] = isset($_POST['summary']) ? CH_SC.$_POST['summary'].DIG_EC.EN_SC.$en_summary.DIG_EC : '';
		$en_title = yf_empty($_POST['en_title'], '', true);
		$en_author = yf_empty($_POST['en_author'], '', true);
		$en_content = yf_empty($_POST['en_content'], '', true);
		$title = yf_empty($_POST['title'], Lang('KEY_EMPTY'));
		$info['title'] = isset($_POST['title']) ? CH_SC.$title.DIG_EC.EN_SC.$en_title.DIG_EC : '';
		$info['abstract'] = yf_empty($_POST['abstract'], '', true);
		$info['flags'] = is_array($_POST['flags']) ? implode(',', $_POST['flags']) : $_POST['flags'];
		$info['keywords'] = yf_empty($_POST['keywords'], '', true);
		$info['author'] = isset($_POST['author']) ? CH_SC.$_POST['author'].DIG_EC.EN_SC.$en_author.DIG_EC : '';
		$info['source'] = yf_empty($_POST['source'], '', true);
		$info['content'] = isset($_POST['content']) ? CH_SC.$_POST['content'].DIG_EC.EN_SC.$en_content.DIG_EC : '';
		$info['ordering'] = yf_empty($_POST['ordering'], 0, true);
		$info['hits'] = yf_empty($_POST['hits'], 0, true);
		$info['source_url'] = yf_empty($_POST['source_url'], '', true);
		$info['page_title'] = yf_empty($_POST['page_title'], '', true);
		$info['created'] = empty($_POST['created']) ? yf_time() : strtotime($_POST['created']);
		$info['thumb'] = yf_empty($_POST['thumb'], '', true);
		$info['is_show'] = yf_empty($_POST['is_show'], 1, true);
		if(!empty($_POST['photo']))
		{
			$photo = is_array($_POST['photo']) ? $_POST['photo'] : array($_POST['photo']);
		}
		else 
		{
			$photo = array();
		}
		$info['form_id'] = yf_empty($_POST['form_id'], 0, true);
		
		$pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern, stripslashes($info['content']), $match);
		
		if($match) {
			$file = array('name' => '', 'type' => 'image/png', 'tmp_name' => '', 'size' => '', 'error' => 0);
			//上传word图片-start
			foreach($match[1] as $k => $v)
			{
				$pattern2 = '/^(file:\/\/).*$/';
				if(preg_match($pattern2, $v))
				{
					$file['name'] = basename($v);
					$ex = explode(".", $file['name']);
					$ext = end($ex);
				   	switch($ext)
				  	{
    		         	case "png":
    		         		$file['type'] = 'image/png'; 
    		         		break;
    		         	case "jpeg":
    		         		$file['type'] = 'image/jpeg';
    		         		break;
    		         	case "jpg":
    		         		$file['type'] = 'image/jpeg';
    		         		break;
    		          	case "gif":
    		          		$file['type'] = 'image/gif';
    		          		break;
    	          	}
					$file['tmp_name'] = $v;
					
					$thumb_url = Model('image')->upload_image($file, 'article/'.date('Ym'));
					if($thumb_url)
					{
						$info['content']=str_replace(addslashes($v), addslashes($thumb_url), $info['content']);
					}
				}
				else 
				{
					//对于已经上传的图片，自动截取文章第一个图片为缩略图
					if(empty($info['thumb']))
					{
						$info['thumb'] = $match[1][0];
					}
				}
			}
			//上传word图片-end
		}
		if(Model('article')->create_art($info, $photo))
		{
			$data['content']=Lang('SUCCESS_ADD');
			$data['error']=0;
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
	public function page()
	{
		$this->admin_priv('edit_news');
		$this->assign('forms_list',Model('forms')->get_all_forms());
		$this->assign('parent_list',Model('menu')->get_cat_list());
		$cat_id=isset($_GET['c'])?intval($_GET['c']):'';
		$id=Model('article')->get_page_art($cat_id);//查找与该分类相关的文章
		if($id)
		{
			$art_info=Model('article')->get_art($id);
			$this->assign('id',$id);
			$this->assign('art',$art_info);
			$this->assign('action','modified_art');
		}
		else
		{
			$art_info['cat_id']=$cat_id;
			$this->assign('art',$art_info);
			$this->assign('action','new_art');
		}
		$this->display('article.html');
	}
}